package com.example.demo.guanchazhe;

import java.util.ArrayList;

public class WeatherData implements Subject {

    private float temperature;
    private float pressure;
    private float humidity;
    private CurrentConditions currentConditions;
    private ArrayList<Observer> observerArrayList;

    public float getTemperature() {
        return temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public WeatherData() {
        observerArrayList = new ArrayList<>();
    }

    //当前数据由更新时 就调用setData
    public void setData(float temperature, float pressure, float humidity) {
        this.humidity = humidity;
        this.pressure = pressure;
        this.temperature = temperature;
        dateChange();
    }

    public  void dateChange(){
        //调用借入方的更新
        notifyObserver();
    }


    @Override
    public void registerObserver(Observer o) {
        observerArrayList.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observerArrayList.remove(o);
    }

    @Override
    public void notifyObserver() {
        for (int i = 0; i < observerArrayList.size(); i++) {
            observerArrayList.get(i).update(this.temperature,this.pressure,this.humidity);
        }
    }
}
